<?php

/**
 * This is the model class for table "user_address".
 *
 * The followings are the available columns in table 'user_address':
 * @property integer $id
 * @property string $address_no
 * @property string $soi
 * @property string $village
 * @property string $street
 * @property string $sub_district
 * @property integer $district_id
 * @property integer $province_id
 * @property integer $nationality_id
 * @property string $zipcode
 * @property string $created_date
 * @property string $updated_date
 * @property integer $user_id
 * @property integer $address_type_id
 *
 * The followings are the available model relations:
 * @property User $user
 * @property Province $province
 * @property AddressType $addressType
 * @property District $district
 * @property Nationality $nationality
 */
class UserAddress extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return UserAddress the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'user_address';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('province_id, nationality_id, user_id, address_type_id', 'required'),
            array('district_id, province_id, nationality_id, user_id, address_type_id', 'numerical', 'integerOnly'=>true),
            array('address_no', 'length', 'max'=>45),
            array('soi', 'length', 'max'=>200),
            array('village, street, sub_district', 'length', 'max'=>100),
            array('zipcode', 'length', 'max'=>5),
            array('created_date, updated_date', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, address_no, soi, village, street, sub_district, district_id, province_id, nationality_id, zipcode, created_date, updated_date, user_id, address_type_id', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'user' => array(self::BELONGS_TO, 'User', 'user_id'),
            'province' => array(self::BELONGS_TO, 'Province', 'province_id'),
            'addressType' => array(self::BELONGS_TO, 'AddressType', 'address_type_id'),
            'district' => array(self::BELONGS_TO, 'District', 'district_id'),
            'nationality' => array(self::BELONGS_TO, 'Nationality', 'nationality_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'address_no' => 'Address No',
            'soi' => 'Soi',
            'village' => 'Village',
            'street' => 'Street',
            'sub_district' => 'Sub District',
            'district_id' => 'District',
            'province_id' => 'Province',
            'nationality_id' => 'Nationality',
            'zipcode' => 'Zipcode',
            'created_date' => 'Created Date',
            'updated_date' => 'Updated Date',
            'user_id' => 'User',
            'address_type_id' => 'Address Type',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('address_no',$this->address_no,true);
		$criteria->compare('soi',$this->soi,true);
		$criteria->compare('village',$this->village,true);
		$criteria->compare('street',$this->street,true);
		$criteria->compare('sub_district',$this->sub_district,true);
		$criteria->compare('district_id',$this->district_id);
		$criteria->compare('province_id',$this->province_id);
		$criteria->compare('nationality_id',$this->nationality_id);
		$criteria->compare('zipcode',$this->zipcode,true);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('updated_date',$this->updated_date,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('address_type_id',$this->address_type_id);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    /**
     * before save
     */
    public function beforeSave() {
        if ($this->isNewRecord)
            $this->created_date = new CDbExpression('NOW()');
        else
            $this->updated_date = new CDbExpression('NOW()');
    
        return parent::beforeSave();
    }

    /**
     * get Delmark off 
     * @return array()
     */
    public function getAll()
    {
        return $this->findAll('delmark=:delmark', array(':delmark' => false));
    }

    /**
     * get Delmark off and Active on
     * @return array()
     */
    public function getAllActive()
    {
        return $this->findAll('delmark=:delmark AND active=:active', array(
                   ':delmark' => false,
                   ':active'  => true,
               ));
    }

    /**
     * virtual field : active
     * @return boolean
     */
    public function getActiveFlag()
    {
        return $this->active==1 ? 'true' : 'false';
    }



}